
local alien = require 'alien'
local config = require 'config'
local bit = require 'bit'


module (...)


local lib = alien.load(config.libgdk_path)

local GdkEventType = 'int'
local gint8 = 'byte'
local guint32 = 'uint'
local gdouble = 'double'
local gboolean = 'int'
local guint32 = 'uint'
local guint = 'uint'
local gint16 = 'short'

local function Func(name, types)
  local func = lib["gdk_"..name]
  func:types(types)
  _M[name] = func
end

Func("screen_get_default", { ret="pointer" --[[GdkScreen*]] })
Func("screen_get_width", { ret=gint; "pointer" --[[GdkScreen*]] })
Func("screen_get_height", { ret=gint; "pointer" --[[GdkScreen*]] })

EventMotion = alien.defstruct{
  {"type", GdkEventType};
  {"window", "pointer" --[[GdkWindow*]]};
  {"send_event", gint8};
  {"time", guint32};
  {"x", gdouble};
  {"y", gdouble};
  {"axes", "pointer" --[[gdouble*]]};
  {"state", guint};
  {"is_hint", gint16};
  {"device", "pointer" --[[GdkDevice]]};
  {"x_root", gdouble};
  {"y_root", gdouble};
}

EXPOSURE_MASK		= bit.lshift(1, 1)
POINTER_MOTION_MASK	= bit.lshift(1, 2)
POINTER_MOTION_HINT_MASK	= bit.lshift(1, 3)
BUTTON_MOTION_MASK	= bit.lshift(1, 4)
BUTTON1_MOTION_MASK	= bit.lshift(1, 5)
BUTTON2_MOTION_MASK	= bit.lshift(1, 6)
BUTTON3_MOTION_MASK	= bit.lshift(1, 7)
BUTTON_PRESS_MASK		= bit.lshift(1, 8)
BUTTON_RELEASE_MASK	= bit.lshift(1, 9)
KEY_PRESS_MASK		= bit.lshift(1, 10)
KEY_RELEASE_MASK		= bit.lshift(1, 11)
ENTER_NOTIFY_MASK		= bit.lshift(1, 12)
LEAVE_NOTIFY_MASK		= bit.lshift(1, 13)
FOCUS_CHANGE_MASK		= bit.lshift(1, 14)
STRUCTURE_MASK		= bit.lshift(1, 15)
PROPERTY_CHANGE_MASK	= bit.lshift(1, 16)
VISIBILITY_NOTIFY_MASK	= bit.lshift(1, 17)
PROXIMITY_IN_MASK		= bit.lshift(1, 18)
PROXIMITY_OUT_MASK	= bit.lshift(1, 19)
SUBSTRUCTURE_MASK = bit.lshift(1, 20)
SCROLL_MASK = bit.lshift(1, 21)
ALL_EVENTS_MASK = 0x3FFFFE

Rectangle = alien.defstruct{
  {"x", "int"};
  {"y", "int"};
  {"width", "int"};
  {"height", "int"};
}

Func("window_invalidate_rect", { ret="void"; "pointer" --[[GdkWindow*]], "pointer" --[[const GdkRectangle*]], gboolean --[[invalidate_children]] })
Func("window_process_updates", { ret="void"; "pointer" --[[GdkWindow*]], gboolean --[[update_children]] })

if (alien.platform == "windows") then
  Func("win32_drawable_get_handle", { ret="pointer" --[[HGDIOBJ]]; "pointer" --[[GdkDrawable*]] })
end

local GdkGrabStatus = 'int'
local GdkEventMask = 'int'

Func("pointer_grab", { ret=GdkGrabStatus;
  "pointer" --[[GdkWindow*]], gboolean --[[user_events]], GdkEventMask, "pointer" --[[GdkWindow* confine_to]], "pointer" --[[GdkCursor* cursor]],
  "uint" --[[time_]] })

Func("pointer_ungrab", { ret="void"; "uint" --[[time_]] })
  
CURRENT_TIME = 0

Func("screen_get_root_window", { ret="pointer" --[[GdkWindow*]]; "pointer" --[[GdkScreen*]] })

Func("window_get_pointer", { ret="pointer" --[[GdkWindow*]];
  "pointer" --[[GdkWindow*]], "ref int" --[[x]], "ref int" --[[y]], "pointer" })
